
import Preview from './components/preview/index';
import HederTop from '@/components/header-top'

export default {
  name: 'repay',
  components: {
    Preview,
    HederTop
  },
  props:{
    typeMode:{
      default:"page"
    },
    orderid:{
      default:''
    }
  },
  data () {
    return {
      productId:'',
      previewData: {},
      type: '',
      showConfirmDialog: false, // 显示确认弹框
      repayForm: {
        repayType: '711', // 还款方式
        amount: '', // 还款金额
      },
      repayCodeImg: '',
      showRepayCode: false,
      activeTab: '', // 当前还款方式
      // SKYChannelList: [
      //   { code: 10, name: '7-11', value: '7-11', show: false },
      //   { code: 9, name: 'RD Pawnshop', value: 'RD', show: false },
      //   { code: 1, name: 'M.Lhuilier', value: 'ML', show: false },
      //   { code: 6, name: 'GCASH', value: 'GCASH', show: false },
      //   { code: 8, name: 'PayMaya', value: 'PayMaya', show: false }
      // ],
      // MSPChannelList: [
      //   { code: 1, name: 'M.Lhuilier', value: 'ML', show: false },
      //   { code: 9, name: 'RD Pawnshop', value: 'RD', show: false },
      //   { code: 10, name: '7-11', value: '7-11', show: false },
      //   { code: 6, name: 'GCASH', value: 'GCASH', show: false }
      // ],
      // XenditChannelList: [
      //   { code: 10, name: '7-11', value: '7-11', minMoney: 50, show: false },
      //   { code: 15, name: 'GrabPay', value: 'GrabPay', minMoney: 1, show: false },
      //   { code: 6, name: 'GCASH', value: 'GCASH', minMoney: 1, show: false },
      //   { code: 8, name: 'PayMaya', value: 'PayMaya', minMoney: 1, show: false }
      // ],
      XRepayChannel: { code: 10, name: '', value: '7-11', minMoney: 50, show: false },
      couponAmount: 0, // 使用的优惠券金额
      onlineList: [], // 线上还款
      offlineList: [], // 线下还款
      instapayList: [], // Instapay还款
    };
  },
  methods: {
    // 根据订单号获取订单信息
    getOrderByOrderId() {
      const params = {
        orderNo: this.orderId,
      };
      this.$api.orderInfo(params).then(res => {
        const { status, error, msg, data } = res;
        if ('1' === status && ('00000000' === error || '0000' === error) && data) {
          this.previewData = data;
          const contractNumber = data.contractNumber;
          if (!contractNumber) {
            this.type = 'Xendit';
          } else if (contractNumber.indexOf('SKY') >= 0) {
            this.type = 'SKY';
          } else if (contractNumber.indexOf('MSP') >= 0) {
            this.type = 'MSP';
          }
        } else {
          this.$toast(msg);
        }
      });
    },
    /*
    // 跳转还款帮助页
    goHelp() {
      this.$router.push({
        path: '/help',
      });
    },
    // 确认还款信息弹框关闭前 restRepay
    beforeClose(action, done) {
      if ('confirm' === action) {
        const amount = this.repayForm.amount;
        const { restRepay } = this.previewData;

        if (/^[1-9]\d*$/.test(amount)) {
          if ('Xendit' === this.type) {
            // Xendit还款方式
            const minMoney = this.XRepayChannel.minMoney;
            if (amount <= minMoney) {
              this.$toast(this.$tc('repayCodeImgAmountTips1', 1, { amount: minMoney })); // 最小还款金额要大于minMoney
              done(false);
              return;
            }
            const params = {
              orderId: this.orderId,
              amount,
              channelCode: this.XRepayChannel.code,
              payinType: 'REPAY', // EXTENSION:展期 REPAY:还款
            };
            if (this.XRepayChannel.code === 10) {
              const config = {
                responseType: 'blob', // 设定获取返回数据内容为blob类型
              };
              this.$api.createXendit711Barcode(params, config).then(res => {
                // 文件流转换为图片
                const file = new FileReader();
                file.onload = event => {
                  this.repayCodeImg = event.target.result;
                  this.showRepayCode = true;
                };
                file.readAsDataURL(res);
                // 完成后关闭弹框
                done();
              });
            } else {
              this.$api.createXenditEWalletCharge(params).then(res => {
                const { status, data } = res;
                if ('1' === status && data) {
                  // window.location.href = data.checkoutUrl;
                  // 用APP打开浏览器页面
                  this.$hybrid.action('openBrowser', {
                    param: {
                      url: data.checkoutUrl,
                      type: '0',
                    },
                    callback: (res) => {
                      console.log(res);
                    }
                  });
                  // 完成后关闭弹框
                  done();
                } else {
                  done(false);
                }
              });
            }
          } else {
            // 当挂账金额大于500时，限制输入的最小还款金额不得小于等于500；
            if (restRepay > 500 && amount <= 500) {
              this.$toast(this.$tc('repayCodeImgAmountTips1', 1, { amount: 500 })); // 最小还款金额要大于500
              done(false);
              return;
            }
            // 当挂账金额小于等于500，大于0时，限制输入的还款金额必须大于等于挂账金额；
            if (restRepay <= 500 && restRepay > 0 && amount < restRepay) {
              this.$toast(this.$tc('repayCodeImgAmountTips2', 1, { amount: restRepay })); // 还款金额必须大于等于挂账金额
              done(false);
              return;
            }
            // 当挂账金额为0时，限制输入的最小还款金额不得小于等于200
            if (restRepay <= 0 && amount <= 200) {
              this.$toast(this.$tc('repayCodeImgAmountTips3')); // 最小还款金额不得小于等于200
              done(false);
              return;
            }
            const params = {
              orderId: this.orderId,
              amount,
              // orderId: '466327506991235072',
              // amount: '2100',
            };
            const config = {
              responseType: 'blob', // 设定获取返回数据内容为blob类型
            };
            this.$api.barcode(params, config).then(res => {
              // 文件流转换为图片
              const file = new FileReader();
              file.onload = event => {
                this.repayCodeImg = event.target.result;
                this.showRepayCode = true;
              };
              file.readAsDataURL(res);
              // 完成后关闭弹框
              done();
            });
          }
        } else {
          this.$toast(this.$tc('plsInputCorrectAmount')); // 请输入正确的还款金额
          done(false);
        }
      } else {
        done();
      }
    },
    // 显示还款信息输入确认弹框
    handleShowConfirmDialog() {
      this.showConfirmDialog = true;
      this.repayForm.amount = '';
      if ('Xendit' === this.type) {
        this.repayForm.amount = +this.previewData.restRepay - this.couponAmount;
        this.XenditChannelList.forEach(item => {
          if (item.value === this.activeTab) {
            this.XRepayChannel = item;
          }
        });
      }
    },
    // 获取还款方式
    getRepayChannelConfig() {
      this.$api.getRepayChannelConfigForApp().then(res => {
        const { status, data } = res;
        if ('1' === status && data && data.length) {
          const type = data[0].paymentCompany;
          let channelList = [];
          if (type.indexOf('MSP') > -1) {
            channelList = this.MSPChannelList;
          } else if (type.indexOf('SkyPay') > -1) {
            channelList = this.SKYChannelList;
          } else if (type.indexOf('Xendit') > -1) {
            channelList = this.XenditChannelList;
          }
          data.forEach(item => {
            channelList.forEach(channel => {
              if (item.channelCode === channel.code) {
                channel.show = true;
              }
            });
          });
          channelList.forEach(channel => {
            if (channel.show && !this.activeTab) {
              this.activeTab = channel.value;
              if (type.indexOf('Xendit') > -1) {
                this.XRepayChannel = channel;
              }
            }
          });
        }
      });
    },
    */
    // 使用了优惠券
    handleCouponAmount(amount) {
      this.couponAmount = amount;
    },
    // 前往还款步骤页面
    goRepayStep(channel) {
      const query = this.$route.query;
      query.repayType = channel.name;
      query.channelCode = channel.channelCode;
      query.companyCode = channel.paymentCompanyCode;
      this.$router.push({
        path: '/repay-detail',
        query,
      });
    },
    // 获取还款方式配置
    getRepayChannel() {
      this.$api.getMixRepayConfig({productId:this.productId}).then(res => {
        const { status, data, error } = res;
        if ('1' === status && '00000000' === error && data) {
          const { online, offline, instapay } = data;
          this.onlineList = online || [];
          this.offlineList = offline || [];
          this.instapayList = instapay || [];
        } else {
          // this.$toast(msg);
        }
      });
    },
  },
  created () {
    // this.$hybrid.action('ee', {
    //   content: 'repay_page_in',
    //   value: '',
    // });
    this.orderId = this.$route.query.orderId;
    this.productId = this.$route.query.productId;
    this.query = this.$route.query;
    if(this.type=='component'){
      this.orderId = this.orderid;
      this.query = {
        orderId:this.orderid
      }
    } 
    this.getOrderByOrderId();
    // this.getRepayChannelConfig();
    this.getRepayChannel();
  }
};
